// miniprogram/components/scoring/scoring.js

const app = getApp()
Component({



  properties: {

    isFav: {
      type: Boolean,
      value: false
    },

    playerorteam: {
      type: String,
      value: "0"
    },

    Top: {
      type: Int16Array,
      value: 9999
    },
    baseimgaeUrl: {
      type: String,
      value: ""
    },
    // 这里定义了id属性，属性值可以在组件使用时指定
    selectid: {
      type: Number,
      value: 0
    },
    matchinfo: {
      type: Object,
      value: undefined,
      observer: function (newVal, oldVal) {

        this._matchinfopropertyChange(newVal, oldVal);

      } // 属性被改变时执行的函数（可选），也可以写成在methods段中定义的方法名字符串, 如：'_propertyChange'

    },
    matchleaders: {
      type: Object,
      value: undefined,
      observer: function (newVal, oldVal) {
        this._matchleaderspropertyChange(newVal, oldVal);
      } // 属性被改变时执行的函数（可选），也可以写成在methods段中定义的方法名字符串, 如：'_propertyChange'

    }
  },

  lifetimes:{
      attached:function(){
          console.log("created",app.globalData.bannerAd );
          this.setData({
            bannerAd:app.globalData.bannerAd ,
          });
      },

  },
  /**
   * 页面的初始数据
   */
  data: {
    staticUrl: app.globalData.staticUrl,
    matchinfo: undefined,
    currentpageheight: 100, //设置高度
    currentTab: 0,
    selectplayerid: undefined,
    selectteamid: undefined,
    teamorplayer: 0, //0为个人1为team
    Favstarplayers: undefined,
    starplayeridlists: undefined,
    top: 0,
    istopnav: false,
    bannerAd:app.globalData.bannerAd ,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  
  /**
   * 用户点击球员姓名
   */
  onplayerclick: function (e) {

    this.setData({
      selectplayerid: e.currentTarget.dataset.id,
      teamorplayer: 0
    });
    if (this._openplayerCard != undefined) {
      this._openplayerCard(e);
    }
  },

  /**
   * 用户点击团队姓名
   */
  onteamclick: function (e) {
    this.setData({
      selectteamid: e.currentTarget.dataset.id,
      teamorplayer: 1
    });
    if (this._openTeamCard != undefined) {
      this._openplayerCard(e);
    }
  },



  /**
   * 组件的方法列表
   * 更新属性和数据的方法与更新页面数据的方法类似
   */
  methods: {
    //打开playerscorecard 页面 
    _openplayerCard(e) {
      this.setData({
        selectplayerid: e.currentTarget.dataset.id,
        teamorplayer: 0
      });
      var playername = e.currentTarget.dataset.playername;
      let id = (this.data.teamorplayer == 0) ? this.data.selectplayerid : this.data.selectteamid;
      // 微信小程序中是通过triggerEvent来给父组件传递信息的
      this.triggerEvent('_openplayerCard', {
        playerorteam: this.data.teamorplayer,
        selectid: id,
        playername: playername,
        matchinfo: JSON.stringify(this.data.matchinfo)
      }) // 将参数的形式传递给父组件

    },

    _openteamCard:function(e){
      this.setData({
        selectteamid: e.currentTarget.dataset.id,
        teamorplayer: 0
      });
      var teamname = e.currentTarget.dataset.teamname;
      let id =e.currentTarget.dataset.id;
      // 微信小程序中是通过triggerEvent来给父组件传递信息的
      this.triggerEvent('_openteamCard', {
        
        selectid: id,
        teamname: teamname,
        matchinfo: JSON.stringify(this.data.matchinfo)
      }) // 将参数的形式传递给父组件
    },


    _matchinfopropertyChange: function (newVal, oldVal) {

      this.fillheight();
      this.setData({
        matchinfo: newVal,
        bannerAd:app.globalData.bannerAd
      });
      console.log("this.databannerAd");
      console.log(this.data.bannerAd);

    },

    _matchleaderspropertyChange: function (newVal, oldVal) {
      this.fillheight();
      this.setData({

        bannerAd:app.globalData.bannerAd
      });
      if (this.data.isFav) {
        this.GetFavStarplayer();
      }
      console.log("this.databannerAd");
      console.log(this.data.bannerAd);
    },

    ///填充swiper的高度，它不能自适应
    fillheight: function () {
      var that = this;
      var obj = this.createSelectorQuery().select("#vscoring" + that.data.currentTab);

      obj.boundingClientRect(function (rect) {
        if (rect == null || rect.height == null) {
          return;
        }
        
        

          that.setData({
            currentpageheight: (rect.height) // 节点的高度
          });
        
        

      }).exec();


    },

    //  tab切换逻辑
    swichNav: function (e) {
      var that = this;
      if (that.data.currentTab === e.target.dataset.current) {

        return false;
      } else {
        that.setData({
          currentTab: e.target.dataset.current
        })

        that.fillheight();
      }
    },

    bindChange: function (e) {
      var that = this;
      that.setData({
        currentTab: e.detail.current
      });
      that.fillheight();
    },



    ///设置星标球员的绑定事件
    _FavStartplayerop: function (e) {
      var playerid = e.target.dataset.playerid;
      var matchid = this.data.matchinfo.matchid;
      var starplayers = wx.getStorageSync('stars');
      console.log(JSON.parse('[{"matchid":' + matchid + ',"playerid":' + playerid + '}]'))
      console.log(starplayers == "")
      var isstarplayers = false;
      var fisrtjsonfav = JSON.parse('[{"matchid":' + matchid + ',"playerid":' + playerid + '}]');
      var jsonfav = JSON.parse('{"matchid":' + matchid + ',"playerid":' + playerid + '}');
      if (starplayers == undefined || starplayers == "") { //新增一个
        wx.setStorageSync('stars', fisrtjsonfav);

      } else {
        if (starplayers.length > 0) {
          for (var starplayer in starplayers) {
            if (starplayers[starplayer].matchid == matchid &&
              starplayers[starplayer].playerid == playerid) { //执行删除操作
              starplayers.splice(starplayer, 1); //删除改元素
              isstarplayers = true;



              console.log("starplayers扎到了删掉之后");
              console.log(starplayers);


              break;
            }

          }
          if (!isstarplayers) { //没有找到新增
            starplayers.push(jsonfav);
            wx.setStorageSync('stars', starplayers);
          } else { //找到了删除
            console.log("删除starplayers缓存");
            console.log(starplayers);
            if (starplayers.length <= 0) {
              wx.removeStorageSync('stars');
              console.log("stars缓存");
              console.log(starplayers);

            } else {
              wx.setStorageSync('stars', starplayers);
              console.log("stars缓存");
              console.log(starplayers);
            }

            this.deletFavplayers(playerid);

          }
        } else {

          starplayers.push(jsonfav);
          wx.setStorageSync('stars', starplayers);
        }
      }

      this.GetFavStarplayer();
    },
    ///获取缓存的星标用户
    GetFavStarplayer: function () {

      var allstarplayers = wx.getStorageSync('stars'); //stars对象里面存储了matchid和playerid
      console.log("缓存获取allstarplayers");
      console.log(allstarplayers);
      if (allstarplayers != "" && allstarplayers.length > 0) {
        var matchid = this.data.matchinfo.matchid;
        //取出该场比赛的player

        var starplayers = undefined;
        console.log(allstarplayers);
        for (var starplayersindex = 0; starplayersindex < allstarplayers.length; starplayersindex++) {
          if (allstarplayers[starplayersindex].matchid == matchid) {
            if (starplayers == undefined || starplayers.length <= 0) {
              starplayers = JSON.parse('[{"matchid":' + matchid + ',"playerid":' + allstarplayers[starplayersindex].playerid + '}]');;

            } else {
              starplayers.push(allstarplayers[starplayersindex]);
            }
          }
        }

      } else {
        this.setData({
          Favstarplayers: null

        });
      }
      this.SetFavPlayers(starplayers);

    },

    deletFavplayers(playerid) {
      var that = this;
      var matchleaders = that.data.matchleaders;
      for (var groupindex = 0; groupindex < matchleaders.length; groupindex++) {

        if (matchleaders[groupindex].leaders != undefined && matchleaders[groupindex].leaders.length > 0) {
          for (var leaderindex = 0; leaderindex < matchleaders[groupindex].leaders.length; leaderindex++) {


            if (playerid == matchleaders[groupindex].leaders[leaderindex].playerID) {
              if (matchleaders[groupindex].leaders[leaderindex].newParam == undefined) {
                matchleaders[groupindex].leaders[leaderindex].newParam = 'isFav';
              }

              matchleaders[groupindex].leaders[leaderindex].isFav = false;
              that.setData({
                matchleaders: matchleaders
              })
              return;
            }


          }
        }
      }
    },

    SetFavPlayers(starplayers) {
      console.log("获取当前比赛所有的starplayers");
      console.log(starplayers);
      var starplayerleaders = null;
      var matchleaders = this.data.matchleaders;
      if (typeof starplayers == 'object' && starplayers.length > 0) {


        if (this.data.matchinfo.isGroup == 1) { //分组别


          for (var groupindex = 0; groupindex < matchleaders.length; groupindex++) {

            if (matchleaders[groupindex].leaders != undefined && matchleaders[groupindex].leaders.length > 0) {
              for (var leaderindex = 0; leaderindex < matchleaders[groupindex].leaders.length; leaderindex++) {

                for (var starplayerindex = 0; starplayerindex < starplayers.length; starplayerindex++) {
                  if (starplayers[starplayerindex].playerid == matchleaders[groupindex].leaders[leaderindex].playerID) {
                    if (starplayerleaders == undefined || starplayerleaders.length == 0) { //第一次加载先复制过去
                      starplayerleaders = new Array();
                      starplayerleaders.push(matchleaders[groupindex].leaders[leaderindex]);
                    } else {
                      starplayerleaders.push(matchleaders[groupindex].leaders[leaderindex]);
                    }
                    matchleaders[groupindex].leaders[leaderindex].newParam = 'isFav';
                    matchleaders[groupindex].leaders[leaderindex].isFav = true;
                  }

                }
              }
            }
          }

        } else { ///不分组别
          for (var groupindex = 0; groupindex < matchleaders.length; groupindex++) {

            if (matchleaders[groupindex].leaders != undefined && matchleaders[groupindex].leaders.length > 0 && matchleaders[groupindex].isteam!=0) {
              for (var leaderindex = 0; leaderindex < matchleaders[groupindex].leaders.length; leaderindex++) {

                for (var starplayerindex = 0; starplayerindex < starplayers.length; starplayerindex++) {
                  if (starplayers[starplayerindex].playerid == matchleaders[groupindex].leaders[leaderindex].playerID) {
                    if (starplayerleaders == undefined || starplayerleaders.length == 0) { //第一次加载先复制过去
                      starplayerleaders = new Array();
                      starplayerleaders.push(matchleaders[groupindex].leaders[leaderindex]);
                    } else {
                      starplayerleaders.push(matchleaders[groupindex].leaders[leaderindex]);
                    }
                    matchleaders[groupindex].leaders[leaderindex].newParam = 'isFav';
                    matchleaders[groupindex].leaders[leaderindex].isFav = true;
                  }

                }
              }
            }

          }
        }
    
      }

      console.log("修改之后的对象matchleaders");
      console.log(matchleaders);
      this.setData({
        Favstarplayers: starplayerleaders,
        matchleaders: matchleaders
      });
    },


    scrollTop(e) {
      let that = this;
      if (e.detail.scrollTop <= 0 && that.data.istopnav) {
        that.setData({
          istopnav: false
        })
      } else {

      }

    },


    //控制回到顶部按钮的显示与消失
    _scrollTopFun(e) {

      let that = this;
      var objtitle = that.createSelectorQuery();
      objtitle.select("#vscoring" + that.data.currentTab).boundingClientRect(function (rect) {
        var top = rect.top;
        console.log(top);
        if (top <= 0) {
          that.setData({
            istopnav: true
          })

        } else {
          that.setData({
            istopnav: false
          })
        }
      }).exec();

    },

    openmedia:function(e){
      console.log('openmedia',e);
        if(this.data.bannerAd.mediasrc){
        
              this.setData({isplay:!this.data.isplay})
              if(this.data.isplay){
                var videoplay = wx.createVideoContext('video', this);
                console.log('openmedia',videoplay);
                videoplay.play();
              }
        }
    },

  },




})